<template>
  <base-page-view class-name="page-container want-to-buy-container">
    <template v-if="!loading">
      <view v-if="article.user_id" class="article-item" @click="downloadApp">
        <view class="user">
          <view class="avatar">
            <b-image v-if="article.head_img_url" :src="article.head_img_url" mode="aspectFill" />
            <b-image v-else src="/static/images/app_logo.png" mode="aspectFill" />
          </view>
          <view class="sub-item">
            <view class="nickname">{{ article.nick_name }}</view>
            <view>{{ article.created_at | formatDate('MM-dd') }}</view>
          </view>
          <view class="attention-btn" @click="downloadApp">+ 关注</view>
        </view>
        <view class="content">
          <view class="text">{{ article.content }}</view>
          <view v-if="article.resource_object_list && article.resource_object_list.length" class="resource">
            <view v-for="(imgItem, imgIndex) in article.resource_object_list" :key="imgIndex" class="resource-item">
              <video v-if="imgItem.videoPreviewImg" :src="imgItem.resourceUrl" :poster="imgItem.videoPreviewImg"></video>
              <b-image v-else :src="imgItem.resourceUrl" mode="aspectFit" />
            </view>
          </view>
        </view>
        <view v-if="article.goods && article.goods.length" class="goods">
          <view v-for="(goodsItem, goodsIndex) in article.goods" :key="goodsIndex" class="goods-item" @click.stop="toGoodsDetail(goodsItem)">
            <view class="goods-image">
              <b-image :src="goodsItem.goods_image_url" mode="aspectFit" />
            </view>
            <view class="goods-detail">
              <view class="goods-name">{{ goodsItem.goods_name }}</view>
              <view class="goods-price">￥{{ goodsItem.price | formatMoney }}</view>
            </view>
          </view>
        </view>
        <view class="num-detail">
          <view class="num-item">
            <image src="/static/images/icon/icon_parise.png" mode="aspectFit" />
            点赞{{ article.praise_num }}次</view>
          <view class="num-item">
            <image src="/static/images/icon/icon_share.png" mode="aspectFit" />
            分享{{ article.share_num }}次
          </view>
        </view>
      </view>
      <view v-else class="empty-state">
        <b-image src="/static/images/app/wantToBuy/empty.png" :is-asset="true" mode="widthFix" />
      </view>
      <view class="download-block" @click="downloadApp">
        <b-image src="/static/images/app/wantToBuy/download-btn.png" :is-asset="true" mode="widthFix" />
      </view>
    </template>
  </base-page-view>
</template>
<script>
import './index.scss'
export default {
  name: 'WantToBuy',
  data() {
    return {
      articleId: '',
      share_code: '',
      loading: true,
      article: {}
    }
  },
  onLoad(option) {
    this.articleId = option.articleId
    this.share_code = option.invite_code || option.share_code
    if (this.articleId) {
      this.fetchData()
    }
  },
  methods: {
    async fetchData() {
      this.loading = true
      const res = await this.$api.appApi.getWantToBuyArticleDetail({ articleId: this.articleId })
      if (res.success && res.data && res.data.length) {
        if (res.data[0].audit_status !== 4) {
          uni.showToast({
            title: '素材已过期',
            icon: 'none'
          })
        } else {
          this.article = res.data[0]
        }
      } else {
        uni.showToast({
          title: '素材不存在',
          icon: 'none'
        })
      }
      this.loading = false
    },
    toGoodsDetail(item) {
      let share_str = ''
      if (this.share_code) {
        share_str = `&share_code=${this.share_code}`
      }
      const urlParams = encodeURIComponent(`/pagesDetail/commodity/index?id=${item.goods_id}&from_type=1&activity_id=&goods_id=${item.goods_id}&goods_type=${item.goods_type}&pack_type=${item.pack_type}${share_str}`)
      const url = `${process.env.VUE_APP_PRE_URL}/pages/share/index?path=${urlParams}`
      console.log(url)
      // #ifdef H5
      window.open(url)
      // #endif
    },
    downloadApp() {
      this.$sensorTrack('CLICKBUTTON', {
        page_name: 'share_grass',
        button_name: 'open'
      })
      const u = navigator.userAgent
      const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
      const url = isAndroid ? 'https://android.myapp.com/myapp/detail.htm?apkName=com.bisinuolan.app&ADTAG=mobile' : 'https://itunes.apple.com/cn/app/id1243270000'
      // #ifdef H5
      window.open(url)
      // #endif
    }
  }
}
</script>
